# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=taglib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.2
pkgrel=1
pkgdesc="A Library for reading and editing the meta-data of several popular audio formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://taglib.org/"
msys2_repository_url="https://github.com/taglib/taglib"
msys2_references=(
  "cpe: cpe:/a:scott_wheeler:taglib"
  "cpe: cpe:/a:taglib:taglib"
)
license=('spdx:LGPL-2.1-or-later' 'spdx:MPL-1.1')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cppunit"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://taglib.github.io/releases/${_realname}-${pkgver}.tar.gz
        fix-finding-cppunit.patch)
sha256sums=('0de288d7fe34ba133199fd8512f19cc1100196826eafcb67a33b224ec3a59737'
            '460e2dd83e6233145e4980a4c63a400e9e8297980e3cfae910dadefa2475e7d0')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/fix-finding-cppunit.patch
}

build() {
  msg "Build shared version"
  mkdir -p "${srcdir}/build-shared-${MSYSTEM}"
  cd "${srcdir}/build-shared-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_BINDINGS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  msg "Build static version"
  mkdir -p "${srcdir}/build-static-${MSYSTEM}"
  cd "${srcdir}/build-static-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_BINDINGS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
    cd "${srcdir}/build-static-${MSYSTEM}"
    ${MINGW_PREFIX}/bin/ctest . || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-static-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}/build-shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
